;-*- coding: utf-8 -*-
; Fritz Kunze's emacs init file.
; 2008-08
; 
; 


;----
; loading custom commands and functions


(message "hey fritz - we are now processing our c:/users/fkunze/.emacs.d/init.el")

(message "hey fritz - we are loading the file c:\\users\\fkunze\\.emacs.d\\init.el")

(message "hey fritz - we are loading our emacs keyboard shortcuts")

(load-file (w32-short-file-name "c:/users/fkunze/.emacs.d/keyboard.el"))


(message "hey fritz - we've finished loading our emacs keyboard shortcuts")

(message "hey fritz - we are loading our emacs_abbrevs.el")

(load-file (w32-short-file-name "c:/users/fkunze/.emacs.d/emacs_abbrevs.el"))

(message "hey fritz - we've finished loading our emacs_abbrevs.el")

(message "hey fritz - we are loading our elisp_util.el")

(load-file (w32-short-file-name "c:/users/fkunze/.emacs.d/elisp_util.el"))

(message "hey fritz - we've finished loading our elisp_util.el")

;;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph    
;;; You can convert an entire buffer from paragraphs to lines by recording a macro that calls ‘unfill-paragraph’ and moves past the blank-line to the next unfilled paragraph and then executing that macro on the whole buffer, ‘C-u 0 C-x e’ (see InfiniteArgument (http://www.emacswiki.org/emacs/InfiniteArgument) ).


(defun unfill-paragraph ()
"Takes a multi-line paragraph and makes it into a single line of text."
 (interactive)
 (let ((fill-column (point-max)))
   (fill-paragraph nil)))

(global-set-key (kbd "M-Q") 'unfill-paragraph)
(message "hey fritz - we've finished loading unfill-paragraph - use it with longlines-mode disabled")

;; To load custom changes to emacs, put them in your own directory and load packages out of this directory first.  This way, emacs will load changes you add, and ignore packages with the same name when it loads from the default load path. 

(add-to-list 'load-path "~/.emacs.d")

;; (autoload 'rst-mode "rst" nil t)

;; (setq auto-mode-alist
;;       (append '(("\\.txt$" . rst-mode)
;;                 ("\\.rst$" . rst-mode)
;;                 ("\\.rest$" . rst-mode)) auto-mode-alist))


;; The recommended way to associate files is to associate
;; them with emacsclientw.exe.  In order for this to work
;; when Emacs is not yet started, you will also need to
;; set the environment variable ALTERNATE_EDITOR to
;; runemacs.exe.  To open files in a running instance of
;; Emacs, you will need to add the following to your init
;; file: (server-start)

(server-start)

(message "hey fritz - we've set up runemacs and emacsclientw.exe")

;;
(message "hey fritz - we are now adding the org mode stuff")
;;
;; Begin by first adding in the latest versions of remember.el
;;
;;
;(setq load-path (cons (w32-short-file-name "C:/bin/mksmin/Remember/remember-2.0") load-path))
;(add-to-list 'load-path "~/.emacs.d/remember-2.0") part of emac 23.1 now
;; 4.8.1 Setting up remember
;; -------------------------

;; The following customization will tell remember to use org files as
;; target, and to create annotations compatible with Org-mode links.

(setq org-directory "c:/bin/mksmin/Org/")
(setq org-default-notes-file "c:/bin/mksmin/Org/.notes")
;(setq remember-annotation-functions '(org-remember-annotation))
;     (setq remember-handler-functions '(org-remember-handler))
;     (add-hook 'remember-mode-hook 'org-remember-apply-template)

(require 'remember)
;;
;; If you are missing the function org-remember-insinuate try these forms
;;
;; (setq remember-annotation-functions '(org-remember-annotation))
;;     (setq remember-handler-functions '(org-remember-handler))
;;     (add-hook 'remember-mode-hook 'org-remember-apply-template)


;;
;; Setting up for PlannerMode

;; Add this to your .emacs:

;;   (require 'remember-planner)
;;   (setq remember-handler-functions '(remember-planner-append))
;;   (setq remember-annotation-functions planner-annotation-functions)
;;
;; Now the stuff for org follows...
;;


;; Org-mode settings
;; you can simply run Org directly from the distribution
;; directory by adding the lisp subdirectory to the Emacs
;; load path.  To do this, add the following line to
;; .emacs:

;;    (setq load-path (cons "~/path/to/orgdir/lisp" load-path))

;; If you plan to use code from the contrib subdirectory,
;; do a similar step for this directory:

;;     (setq load-path (cons "~/path/to/orgdir/contrib/lisp" load-path))

;(setq load-path (cons (w32-short-file-name "C:/bin/mksmin/odistr/org-6.05b/lisp") load-path))
;(setq load-path (cons (w32-short-file-name "C:/bin/mksmin/odistrs/org-6.05b/contrib/lisp") load-path))
;;;(add-to-list 'load-path "~/.emacs.d/org-6.05b/lisp")
;;;(add-to-list 'load-path "~/.emacs.d/org-6.05b/contrib/lisp")
(add-to-list 'load-path "~/.emacs.d/org-6.28e/lisp")
(add-to-list 'load-path "~/.emacs.d/org-6.28e/contrib/lisp")


;; The following lines are always needed.  Choose your own keys.
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
;;
;; Furthermore, you must activate font-lock-mode in Org buffers, because significant functionality depends on font-locking being active.
;; You can do this with either one of the following two lines 
;;   (global-font-lock-mode 1)                     ; for all buffers
;;   (add-hook 'org-mode-hook 'turn-on-font-lock)  ; Org buffers only
;;
;; (add-hook 'org-mode-hook 'turn-on-font-lock)  ; Org buffers only

;;(global-font-lock-mode 1)                     ; use for all buffers
(add-hook 'org-mode-hook 'turn-on-font-lock)  ; Org buffers only
;;;With this setup, all files with extension ‘.org’ will be put into Org mode. As an alternative, make the first line of a file look like this:



(message "hey fritz - org mode loading is done - try out - M-x org-mde see: C:\NP1\Get Organized with Emacs Org-mode.htm")


;;The following file lists some of the customizations I've installed
;;file:///C:/Users/fkunze/Documents/DOCLIB/xahlee%20emacs%20tutorial/xah_emacs_tutorial/emacs/xah_emacs_init.html


;; Set up recentf so I can get a list of recent files when I start
(recentf-mode 1)



;;Question: How to show the cursor's column position?

;;Answer Alt+x column-number-mode. To always have it on, put the following code in your ~/.emacs file: (column-number-mode t).
;;;
;;; I am going to try to use emacs customizations to change the way that I deal with 
;;; column-number-mode and line-number-mode.  Uncomment the following if this fails.
;;;

;;(column-number-mode t)
;;(line-number-mode 1)

(message "hey fritz - you just turned cursor column numbers on permanently")


(defun remove-hard-wrap-paragraph () 
  "Replace line endings into single spaces on the current paragraph."
  (interactive) 
  (let ((fill-column 90002000)) 
    (fill-paragraph nil)))

(defun remove-hard-wrap-region (start end)
  "Replace newline chars in region by single spaces." 
  (interactive "r")
  (let ((fill-column 90002000)) 
    (fill-region start end)))

;;; formerly:
;;; (global-set-key (kbd "M-Q") 'remove-hard-wrap-paragraph)
(global-set-key (kbd "M-R") 'remove-hard-wrap-region)




(message "hey fritz - I've now added two functions, useful for working with text, from xah lee")



(defalias 'list-buffers 'ibuffer)
(message "hey fritz - you just replaced list-buffers with ibuffer")


;;(load (w32-short-file-name "c:/Program Files/acl81/eli/fi-site-init.el"))

;; to find a font
;;(w32-select-font)

;; sets % to the same function in vi
;; (global-set-key "%" 'match-paren)
;;    (defun match-paren (arg)
;;     "Go to the matching paren if on a paren; otherwise insert %."
;;       (interactive "p")
;;       (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
;;             ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
;;             (t (self-insert-command (or arg 1)))))

;; (message "hey fritz - % works the way it does in vi")
;; explorer
    ;; ----------
    ;;; Windows explorer to open current file - Arun Ravindran
    
    (defun explorer ()
      "Launch the windows explorer in the current directory and selects current file"
      (interactive)
      (w32-shell-execute
       "open"
       "explorer"
       (concat "/e,/select," (convert-standard-filename buffer-file-name))))
    
    (global-set-key [f12]         'explorer)        ; F12 - Open Explorer for the current file path

(message "hey fritz - you can now use f-12 to open explorer in the directory containing the file you opened")

(defun compact-uncompact-block ()
  "Remove or add line endings on the current block of text.
This is similar to a toggle for fill-paragraph and unfill-paragraph
When there is a text selection, act on the region.

When in text mode, a paragraph is considerd a block. When in programing
language mode, the block defined by between empty lines.

Todo: The programing language behavior is currently not done.
Right now, the code uses fill* functions, so does not work or work well
in programing lang modes. A proper implementation to compact is replacing
EOL chars by space when the EOL char is not inside string.
"
  (interactive)

  ;; This command symbol has a property “'stateIsCompact-p”, the
  ;; possible values are t and nil. This property is used to easily
  ;; determine whether to compact or uncompact, when this command is
  ;; called again

  (let (bds currentLineCharCount currentStateIsCompact
            (bigFillColumnVal 4333999) (deactivate-mark nil))

    (save-excursion
      ;; currentLineCharCount is used to determine whether current state
      ;; is compact or not, when the command is run for the first time
      (setq currentLineCharCount
            (progn
              (setq bds (bounds-of-thing-at-point 'line))
              (length (buffer-substring-no-properties (car bds) (cdr bds)))    
              ;; Note: 'line includes eol if it is not buffer's last line
              )
            )

      ;; Determine whether the text is currently compact.  when the last
      ;; command is this, then symbol property easily tells, but when
      ;; this command is used fresh, right now we use num of chars of
      ;; the cursor line as a way to define current compatness state
      (setq currentStateIsCompact
            (if (eq last-command this-command)
                (get this-command 'stateIsCompact-p)
              (if (> currentLineCharCount fill-column) t nil)
              )
            )

      (if (and transient-mark-mode mark-active)
          (if currentStateIsCompact
              (fill-region (region-beginning) (region-end))
            (let ((fill-column bigFillColumnVal))
              (fill-region (region-beginning) (region-end)))
            )
        (if currentStateIsCompact
            (fill-paragraph nil)
          (let ((fill-column bigFillColumnVal))
            (fill-paragraph nil))
          )
        )

      (put this-command 'stateIsCompact-p (if currentStateIsCompact
                                              nil t)) ) ) )



(message "hey fritz - you can now use compact-uncompact-block")


;;; this must be loaded after the (load ... eli/fi-site-init.el) 
;;; to solve a compatibilty problem

(setq fi:eli-compatibility-mode nil)

(defun alisp ()
  (interactive)
  (let ((lisp-directory "C:\\Program Files\\acl81\\")
        (lisp-name "mlisp"))
    (fi:common-lisp (format "*%s*" lisp-name)
                    lisp-directory
                    (format "%s\\%s.exe" lisp-directory lisp-name)
                    (list "+B" "+cx" "+M")
                    fi:common-lisp-host
                    (format "%s.dxl" lisp-name))))



(defun add-to-load-path (path-string)
  (message (format "Passed %S..." path-string))
  (if (stringp path-string)
      (when (file-exists-p path-string)
	(message (format "Adding %S to load-path..." path-string))
	(add-to-list 'load-path (expand-file-name path-string)))
    (crs-add-to-load-path (car path-string))
    (if (cdr path-string)
	(crs-add-to-load-path (cdr path-string)))))


(message "Initialization complete") (sit-for 1)
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(abbrev-mode t)
 '(column-number-mode t)
 '(default-frame-alist (quote ((font . "-outline-Courier New-normal-r-normal-normal-21-157-96-96-c-*-iso8859-1") (top . 10) (left . 150) (tool-bar-lines . 1) (menu-bar-lines . 1) (right-fringe) (left-fringe))))
 '(doc-view-ghostscript-program (executable-find "mgs"))
 '(focus-follows-mouse nil)
 '(global-visual-line-mode t)
 '(grep-command "C:\\Windows\\System32\\findstr.exe -n -r  ")
 '(grep-use-null-device t)
 '(gud-pdb-command-name "python -m pdb")
 '(icicle-reminder-prompt-flag 0)
 '(ido-mode (quote both) nil (ido))
 '(inhibit-startup-echo-area-message "fkunze")
 '(inhibit-startup-screen t)
 '(ispell-program-name "C:\\Program Files\\Aspell\\bin\\aspell.exe")
 '(mouse-autoselect-window t)
 '(org-agenda-files (quote ("~/Org/personal.org")))
 '(python-python-command "C:/Python25/python.exe")
 '(recentf-max-saved-items 40)
 '(safe-local-variable-values (quote ((Package . NEWSCADE))))
 '(show-paren-mode t)
 '(text-mode-hook (quote (turn-on-flyspell text-mode-hook-identify))))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )



(put 'upcase-region 'disabled nil)

(put 'downcase-region 'disabled nil)
